<template>
  <div :class="className" :style="{'height':'100%','width':'100%'}" />
</template>

<script>
  import * as echarts from 'echarts'
  require('echarts/theme/macarons') // echarts theme
  import resize from '../../views/dashboard/mixins/resize';
  const animationDuration = 1000

  export default {
    mixins: [resize],
    props: {
      className: {
        type: String,
        default: 'chart'
      },
      width: {
        type: String,
        default: '100%'
      },
      height: {
        type: String,
        default: '500px'
      },
      chartData: {
        type: Object,
        required: true
      }
    },
    data() {
      return {
        chart: null
      }
    },
    mounted() {
      this.$nextTick(() => {
        this.initChart()
      })
    },
    beforeDestroy() {
      if (!this.chart) {
        return
      }
      this.chart.dispose()
      this.chart = null
    },
    methods: {
      initChart() {
        this.chart = echarts.init(this.$el)
        this.setOption(this.chartData)
      },
      setOption({xdata,ydata}={}){
        this.chart.setOption({
          title: {
            text: this.$t('index.overviewOfAlarmVolume'),
            textStyle:{
              fontSize: 16,
              fontWeight:400,
              color:'#fff'
            }
          },
          // tooltip: {
          //   trigger: 'axis',
          //   axisPointer: {
          //     type: 'shadow'
          //   }
          // },
          legend: {
            show:false
          },
          grid: {
            top:40,
            left: '20',
            right: '30',
            bottom: '3%',
            containLabel: true
          },
          xAxis: {
            show:false,
            type: 'value',
            boundaryGap: [0, 0.01],
          },
          yAxis: {
            type: 'category',
            data: ydata,
            axisLine:{
              show:false
            },
            axisLabel:{color:'#fff'},
            axisTick:{show:false}
          },
          series: [
            {
              name: '2011',
              type: 'bar',
              barWidth:12,
              data: xdata,
              showBackground: true,
              backgroundStyle: {
                color: 'rgba(180, 180, 180, 0.2)',
                borderRadius:20,
              },
              label:{
                show:true,
                color:"#fff",
                align:'center',
                verticalAlign:"bottom",
                position:['105%', '-3%']
              },
              itemStyle: {
                borderRadius: [20, 20, 20, 20],
                color: new echarts.graphic.LinearGradient(1, 0, 0, 0, [
                  { offset: 0, color: '#8FC8D3' },
                  { offset: 1, color: '#08203E' },
                ])
              },
            }
          ]
        })
      }
    },
    watch: {
      chartData: {
        deep: true,
        handler(val) {
          this.setOption(val)
        }
      }
    },
  }
</script>
